import { apiService, getCancelToken } from "@/utils/rtkQuery";

/** 用户登录参数 */
export type LoginQuery = {
  /** 用户名 */
  username: string;
  /** 登录密码 */
  password: string;
  /** 自动登录 */
  autoLogin: boolean;
};

/** 用户登录返回 */
export type LoginResponse = {
  accessToken: string;
  uid: string;
  phoneNumber: string;
};

/** 检查登录 */
export type CheckLoginResponse = {
  name: string;
  uid: number;
};

export interface UserBaseResource {
  /**
   * 唯一标识
   */
  id: number;
  /**
   * 包含的资源
   */
  children?: UserMenu[];
  /**
   * 菜单名称
   */
  menuName: string;
  /**
   * 图标
   */
  icon?: string;
  /**
   * 是否在菜单中隐藏
   */
  hiddenInMenu?: boolean;
}

export interface UserButtonResource {
  /**
   * 资源类型
   */
  resourceType: "BUTTON";
}

export interface UserInfoResource {
  name: string;
  avatar: string;
  email: string;
  uid: number | string;
  access: string;
  address: string;
  phone: string;
}

export interface UserMenuResource {
  /**
   * 资源路由
   */
  router?: string;
  /**
   * 资源类型
   */
  resourceType: "MENU";
}

export type UserExtraResource = UserButtonResource | UserMenuResource;

export type UserMenu = UserBaseResource & UserExtraResource;

export const userRest = {
  /** 登录 */
  login: "/public/login",
  /** 登出 */
  logout: "/private/logout",
  /**
   * 检查登录
   */
  checkLogin: "/private/checkLogin",
  /**
   * 检查登录
   */
  userInfo: "/private/user/info",
  /**
   * 用户权限列表
   */
  menus: "/private/user/menu",
};

const { cancelToken, sourceToken } = getCancelToken();
export { cancelToken };

const userApi = apiService
  .enhanceEndpoints({
    addTagTypes: ["UserMenu", "UserValid"],
  })
  .injectEndpoints({
    endpoints: (build) => ({
      login: build.mutation<LoginResponse, LoginQuery>({
        query: (data) => ({
          url: userRest.login,
          method: "POST",
          cancelToken: sourceToken,
          data: data,
        }),
        invalidatesTags: ["UserMenu", "UserValid"],
      }),
      logout: build.mutation<void, void>({
        query: () => ({
          url: userRest.logout,
          method: "POST",
        }),
      }),
      checkLogin: build.query<CheckLoginResponse, void>({
        query: () => ({
          url: userRest.checkLogin,
          method: "POST",
          cancelToken: sourceToken,
        }),
        providesTags: ["UserValid"],
      }),
      getUserInfo: build.query<UserInfoResource, void>({
        query: () => ({
          url: userRest.userInfo,
          method: "POST",
          cancelToken: sourceToken,
        }),
        providesTags: ["UserMenu"],
      }),
      getUserMenus: build.query<UserMenu[], void>({
        query: () => ({
          url: userRest.menus,
          method: "POST",
          cancelToken: sourceToken,
        }),
        providesTags: ["UserMenu"],
      }),
    }),
    overrideExisting: false,
  });

export const {
  useLazyCheckLoginQuery,
  useGetUserInfoQuery,
  useGetUserMenusQuery,
  useLoginMutation,
  useLogoutMutation,
  useLazyGetUserMenusQuery,
} = userApi;
export default userApi;
